<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="admin/public/head::head(~{::title},~{},~{})">
    <title>编辑新闻-新闻管理系统</title>
</head>

<body>
<div class="weadmin-body">
    <form class="layui-form layui-form-pane" id="form" enctype="multipart/form-data" th:object="${checknews}">
        <input type="hidden"  name="id"  th:field="*{id}"/>
        <div class="layui-form-item">
            <label for="title" class="layui-form-label">
                <span class="we-red">*</span>标题
            </label>
            <div class="layui-input-inline" style="width: 80%;">
                <input type="text" id="title" th:field="*{title}"  th:value="${checknews.title}" name="title" required lay-verify="required"
                       placeholder="标题" autocomplete="off"  class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label for="title" class="layui-form-label">
                <span class="we-red">*</span>分类
            </label>
            <div class="layui-input-inline" style="width: 80%;">

                <select name="categoryId">
                    <option th:field="*{categoryId}" th:each="category:${categoryList}" th:value="${category.id}">[[${category.name}]]</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="title" class="layui-form-label">
                <span class="we-red">*</span>主题
            </label>
            <div class="layui-input-inline" style="width: 80%;">
                <input type="text" id="theme"  name="theme"  th:field="*{theme}" required lay-verify="required"
                       placeholder="主题" autocomplete="off"  class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label  class="layui-form-label">
                详细内容
            </label>
            <div class="layui-input-inline" style="width: 80%;min-height: 500px">
                <textarea id="container"  th:field="*{content}" th:text="${checknews.content}" name="content" style="min-height: 300px" ></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <button style="margin-left: 40%;width: 200px" class="layui-btn" lay-filter="edit" lay-submit>审核通过</button>
        </div>
    </form>
</div>
<!-- 百度编辑器配置文件 -->
<script type="text/javascript" th:src="@{/static/common/plugs/ueditor/ueditor.config.js}"></script>
<!-- 百度编辑器编辑器源码文件 -->
<script type="text/javascript" th:src="@{/static/common/plugs/ueditor/ueditor.all.js}"></script>

<script type="text/javascript">
    var ue = UE.getEditor('container');
</script>

<script type="text/javascript">
    layui.use(['form', 'layer','upload', 'jquery', 'admin'], function () {
        var form = layui.form,
            $ = layui.jquery,
            upload = layui.upload,
            layer = layui.layer;
        //图片上传
        var uploadInst = upload.render({
            elem: '#uploadimg'
            , multiple: false//多图上传，不支持IE8/9
            , field: 'file'//后台接收字段名
            , auto: false//不自动上传
            , acceptMime: 'image/*'//（只显示图片文件）
            , choose: function (obj) { //选择图片后执行
                //预读本地文件，如果是多文件，则会遍历。(不支持ie8/9)
                $("#imgBox").empty();
                obj.preview(function (index, file, result) {
                    var img = "<img width='100px' height='100px' src='" + result + "'alt='' class='img-rounded'>";
                    $("#imgBox").append(img);
                });
            }
        });
        //监听提交
        form.on('submit(edit)', function (data) {
            console.log(data.field);
            var formData = new FormData($("#form")[0]);
            $.ajax({
                url: "/check/add",
                type: "post",
                data: formData,
                processData: false,
                contentType: false,
                beforeSend: function(){
                    //返回的参数item，即为当前的input DOM对象
                    index = layer.load(1,{shade: [0.3,'grey']});
                },
                success: function (res) {
                    //关闭遮罩层
                    layer.close(index);
                    if (res.success == true) {
                        layer.msg(res.msg, {icon: 1, time: 1500},function () {
                            refreshParent();
                        });
                    } else {
                        layer.msg(res.msg, {icon: 2, time: 1500});
                    }
                },
                error: function (res) {
                    layer.msg(res, {icon: 2, time: 1500});
                },
                complete: function () {
                }
            });
            return false;
        });
    });
    /*清除回显图片*/
    function clearImg() {
        $("#imgBox").empty();
    }
</script>
</body>

</html>